/*
 * @Author: lvgrape lvgrape@outlook.com
 * @Date: 2024-05-13 16:01:45
 * @LastEditors: lvgrape lvgrape@outlook.com
 * @LastEditTime: 2024-05-15 17:54:32
 * @FilePath: \ZINO_RC_RT\ZINO\hardware\rgb\rgb_pwm.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: lvgrape lvgrape@outlook.com
 * @Date: 2024-05-13 16:01:45
 * @LastEditors: lvgrape lvgrape@outlook.com
 * @LastEditTime: 2024-05-15 11:40:22
 * @FilePath: \ZINO_RC_RT\ZINO\hardware\rgb\rgb_pwm.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <rtthread.h>
#include <rtdevice.h>
#include "zino.h"
#include "rgb.h"

#define DBG_TAG "RGB"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#define pwm_dev_name "pwm4"
#define RGB_DEV_PWM_PERIOD 102400
#define RGB_PWM_DIV 400 //RGB_DEV_PWM_PERIOD/256
#define VIBRATION_PWM_DIV 1024 //RGB_DEV_PWM_PERIOD/100
struct rt_device_pwm *rgb_dev;
/**
 * @brief
 *
 * @param duty 0~100
 */
void vibration_set(uint8_t duty, uint16_t duration)
{
    if (rgb_dev == RT_NULL)
        return;
    if (duty >= 100)
        duty = 100;
    rt_pwm_set(rgb_dev, 4, RGB_DEV_PWM_PERIOD, duty * VIBRATION_PWM_DIV);
    rt_pwm_enable(rgb_dev, 4);
    rt_thread_mdelay(duration);
    rt_pwm_disable(rgb_dev, 4);
}
int rgb_init(void)
{

    rgb_dev = (struct rt_device_pwm *)rt_device_find(pwm_dev_name);
    if (rgb_dev == RT_NULL)
    {
        LOG_E("Can't find %s device!", pwm_dev_name);
        return -RT_ERROR;
    }
    rt_pwm_set(rgb_dev, 1, RGB_DEV_PWM_PERIOD, 0);
    rt_pwm_set(rgb_dev, 2, RGB_DEV_PWM_PERIOD, 0);
    rt_pwm_set(rgb_dev, 3, RGB_DEV_PWM_PERIOD, 0);

    rt_pwm_enable(rgb_dev, 1);
    rt_pwm_enable(rgb_dev, 2);
    rt_pwm_enable(rgb_dev, 3);

    vibration_set(50, 200);
    rgb_set(255, 0, 0);
    rt_thread_mdelay(100);
    rgb_set(0, 255, 0);
    rt_thread_mdelay(10);
    rgb_set(0, 0, 255);
    rt_thread_mdelay(100);
    rgb_set(255, 255, 255);

    LOG_I("rgb init success!");
    return RT_EOK;
}
void rgb_set(uint8_t r, uint8_t g, uint8_t b)
{
    if (rgb_dev == RT_NULL)
        return;
    rt_pwm_set(rgb_dev, 1, RGB_DEV_PWM_PERIOD, RGB_DEV_PWM_PERIOD - b * RGB_PWM_DIV);
    rt_pwm_set(rgb_dev, 2, RGB_DEV_PWM_PERIOD, RGB_DEV_PWM_PERIOD - r * RGB_PWM_DIV);
    rt_pwm_set(rgb_dev, 3, RGB_DEV_PWM_PERIOD, RGB_DEV_PWM_PERIOD - g * RGB_PWM_DIV);

    rt_pwm_enable(rgb_dev, 1);
    rt_pwm_enable(rgb_dev, 2);
    rt_pwm_enable(rgb_dev, 3);
}
void rgb(int argc, char **argv)
{

    if (argc == 1)
    {
        LOG_E("Usage: rgb <r> <g> <b>");
        return;
    }
    int r = atoi(argv[1]);
    int g = atoi(argv[2]);
    int b = atoi(argv[3]);
    rgb_set(r, g, b);
    LOG_I("rgb set: r=%d, g=%d, b=%d", r, g, b);
}
void vibration(int argc, char **argv)
{
    
    if (argc == 1)
    {
        LOG_E("Usage: vibration <duty> <time>");
        return;
    }
    int duty = atoi(argv[1]);
    int time = atoi(argv[2]);
    LOG_I("vibration set: duty=%d time=%d", duty, time);
    vibration_set(duty,time);

}

ZINO_APP_EXPORT(rgb_init);
MSH_CMD_EXPORT(rgb, rgb);
MSH_CMD_EXPORT(vibration, vibration);